package cly.thirdpartyconsumer.service;


import cly.thirdpartyconsumer.client.UpstreamDataClient;
import cly.thirdpartyconsumer.entity.ClyMaterial;
import cly.thirdpartyconsumer.entity.ClyOrder;
import cly.thirdpartyconsumer.entity.SupplierCombinedInfo;
import cly.thirdpartyconsumer.entity.SupplierQualificationCombined;
import cly.thirdpartyconsumer.mapper.ClyMaterialMapper;
import cly.thirdpartyconsumer.mapper.ClyOrderMapper;
import cly.thirdpartyconsumer.mapper.SupplierCombinedInfoMapper;
import cly.thirdpartyconsumer.mapper.SupplierQualificationCombinedMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class DataSyncService {

    @Autowired
    private UpstreamDataClient upstreamDataClient;

    @Autowired
    private ClyMaterialMapper clyMaterialMapper;

    @Autowired
    private ClyMaterialService clyMaterialService;

    @Autowired
    private ClyOrderMapper clyOrderMapper;

    @Autowired
    private ClyOrderService clyOrderService;

    @Autowired
    private SupplierCombinedInfoMapper supplierCombinedInfoMapper;

    @Autowired
    private SupplierCombinedInfoService supplierCombinedInfoService;

    @Autowired
    private SupplierQualificationCombinedMapper supplierQualificationCombinedMapper;

    @Autowired
    private SupplierQualificationCombinedService supplierQualificationCombinedService;

    /**
     * 同步所有数据
     */
    // 上游数据类型列表
    private static final List<String> DATA_TYPES = Arrays.asList("materials", "orders", "suppliers", "qualifications");

    @Transactional(rollbackFor = Exception.class)
    public void syncAllData() {

        for (String type : DATA_TYPES) {
            try {
                switch (type) {
                    case "materials":
                        List<ClyMaterial> materials = upstreamDataClient.fetchData(type, ClyMaterial.class);

                        if (!materials.isEmpty()) {
                            clyMaterialMapper.delete(new QueryWrapper<>()); // 可选：清空旧数据
                            clyMaterialService.saveBatch(materials); // 批量插入
                        }
                        break;
                    case "orders":
                        List<ClyOrder> orders = upstreamDataClient.fetchData(type, ClyOrder.class);
                        if (!orders.isEmpty()) {
                            clyOrderMapper.delete(new QueryWrapper<>());
                            clyOrderService.saveBatch(orders);
                        }
                        break;
                    case "suppliers":
                        // 同步供应商综合信息
                        List<SupplierCombinedInfo> suppliers = upstreamDataClient.fetchData(type, SupplierCombinedInfo.class);
                        if (!suppliers.isEmpty()) {
                            supplierCombinedInfoMapper.delete(new QueryWrapper<>());
                            supplierCombinedInfoService.saveBatch(suppliers);
                        }

                        break;
                    case "qualifications":
                        // 同步供应商资质信息
                        List<SupplierQualificationCombined> qualifications = upstreamDataClient.fetchData(type, SupplierQualificationCombined.class);
                        if (!qualifications.isEmpty()) {
                            supplierQualificationCombinedMapper.delete(new QueryWrapper<>());
                            supplierQualificationCombinedService.saveBatch(qualifications);
                        }
                        break;
                }

            } catch (Exception e) {
                throw e;
            }
        }
    }
}
